/*-----------------------------------------------------
 * 权限子系统-系统日志模块
 * ---------------------------------------------------*/
jx.ready(function () {
    window.api = {
        grid: '/auth/log/grid',
        details: '/auth/log/details',
        export: '/auth/log/export'
    }
    //定义变量
    var $grid = $('#grid');
    var $gridform = $('#gridform');
    var $timeButton = $('#time-horizon > a');
    var $griddatePopover = $('#griddate-popover');
    var $gridadvPopover = $('#gridadvform-popover');
    var gridUrl = jx.apiUrl(api.grid);
    var detailsUrl = jx.apiUrl(api.details);
    var exportUrl = jx.apiUrl(api.export);

    var formSubmit = function(){
        $gridform.find(':submit').click();
    }

    var setTimeHorizon = function ($this) {
        $timeButton.removeClass('active');
        $this.addClass('active');
        var val = $this.attr('data-value');
        if(val=='5') return;
        var $start = $('#operateStartDateTime');
        var $end = $('#operateEndDateTime');
        var cdate = jx.formatDate(new Date());
        switch (val) {
            case '1'://今天
                $start.val(cdate);
                $end.val(cdate);
                formSubmit();
                break;
            case '2'://近7天
                $start.val(jx.formatDate(jx.addDate(new Date(), -7)));
                $end.val(cdate);
                formSubmit();
                break;
            case '3'://近一个月
                $start.val(jx.formatDate(jx.addDate(new Date(), -30)));
                $end.val(cdate);
                formSubmit();
                break;
            case '4'://近三个月
                $start.val(jx.formatDate(jx.addDate(new Date(), -90)));
                $end.val(cdate);
                formSubmit();
                break;
            case '5'://自定义
                break;
        }
    };

    //初始化表格
    var initGrid = function () {
        $grid.jxgrid({
            url: gridUrl,
            onDblClickRow: function (index, row) {
                var id = $grid.jxgrid().getRowId(row);
                if (!id) return;
                jx.detailsDialog({
                    url: detailsUrl,
                    maxmin: true,
                    width: '50%',
                    params: {id: id}
                });
            },
            onBeforeLoad: function (param) {
                if (param.operateEndDateTime && param.operateStartDateTime) {
                    return true;
                }
                return false;
            }
        });
    };

    //初始化事件
    var initEvent = function () {
        $('#btn-datesearch').click(function () {
            if ($('#griddateform').valid()) {
                formSubmit();
                $griddatePopover.popoverX('hide');
            }
        });
        $('#btn-advsearch').click(function () {
            if ($('#gridadvform').valid()) {
                formSubmit();
                $gridadvPopover.popoverX('hide');
            }
        });

        $timeButton.click(function () {
            setTimeHorizon($(this));
        });
        $('.nav-tabs > li').on('click', function () {
            $gridform.find('#operateType').val($(this).data('value'));
            formSubmit();
        });

        $('#btn-export').click(function () {
            exportData();
        });
    };

    //导出数据
    var exportData = function () {
        $(document.body).mask('正在导出数据,请稍等...', 300);
        jx.export(exportUrl, $.extend(jx.serialize($gridform),jx.serialize($('#griddateform')),jx.serialize($('#gridadvform'))));
        $(document.body).unmask();
    };

    //定义表格列格式化函数
    jx.gf.type = function (v, row, index) {
        //操作类型 1: 登录 2: 操作 3: 授权 4: 访问 5: 异常
        var cls = 'default';
        if (v == '1') {
            cls = 'success';
        }
        if (v == '2') {
            cls = 'primary';
        }
        else if (v == '3') {
            cls = 'warning';
        }
        else if (v == '4') {
            cls = 'info';
        }
        else if (v == '5') {
            cls = 'danger';
        }
        return '<span class="label label-' + cls + '">' + row.operateTypeText + '</span>';
    };
    jx.gf.costTime = function (v, row, index) {
        //if (!v) return v;
        return row.costTimeText;
    };
    jx.gf.result = function (v, row, index) {
        if (v == '1') {
            return '<span class="label label-success">成功</span>';
        }
        else {
            return '<span class="label label-danger">失败</span>';
        }
    };

    //初始化
    initGrid();
    initEvent();
    $timeButton.eq(1).click();
});